package epoch.game.core.distr;

import java.io.IOException;

import epoch.game.core.message.SysMessageIds;
import epoch.game.core.serialize.ISerializable;
import epoch.game.core.serialize.IStreamReader;
import epoch.game.core.serialize.IStreamWriter;

/**
 * 内部节点注册消息
 * 
 * @author will
 * @date 2015年11月21日 下午3:26:29
 */
public class NodeRegisterMessage extends NodeMessage implements ISerializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	// 服务节点Id
	private int nodeId;
	// 服务节点名称
	private String nodeName;
	// 服务ip
	private String host;
	// 服务端口
	private int port;
	// 验证公钥
	private String secretKey;
	// 节点类型
	private NodeType type;

	@Override
	public short getId() {
		return SysMessageIds.node_register;
	}

	public int getNodeId() {
		return nodeId;
	}

	public void setNodeId(int nodeId) {
		this.nodeId = nodeId;
	}

	public String getHost() {
		return host;
	}
	
	public void setHost(String host) {
		this.host = host;
	}
	
	public int getPort() {
		return port;
	}
	
	public void setPort(int port) {
		this.port = port;
	}
	
	public String getNodeName() {
		return nodeName;
	}

	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}
	
	public String getSecretKey() {
		return secretKey;
	}

	public void setSecretKey(String secretKey) {
		this.secretKey = secretKey;
	}
	
	public NodeType getType() {
		return type;
	}

	public void setType(NodeType type) {
		this.type = type;
	}

	@Override
	public String toString(){
		StringBuilder buf = new StringBuilder("NodeRegisterMessage[");
		buf.append("nodeId:" + nodeId + ",");
		buf.append("nodeName:" + nodeName + ",");
		buf.append("host:" + host + ",");
		buf.append("port:" + port + ",");
		buf.append("type:" + type + ",");
		if (buf.charAt(buf.length() - 1) == ',')
			buf.deleteCharAt(buf.length()-1);
		buf.append("]");
		return buf.toString();
	}


	@Override
	public void readFrom(IStreamReader reader) throws IOException {
		nodeId = reader.readInt();
		nodeName = reader.readString();
		host = reader.readString();
		port = reader.readInt();
		secretKey = reader.readString();
		String typestr= reader.readString();
		if(typestr!=null && !typestr.trim().isEmpty()){
			type = NodeType.valueOf(typestr.trim().toUpperCase());
		}
	}

	@Override
	public void writeTo(IStreamWriter writer) throws IOException {
		writer.writeInt(nodeId);
		writer.writeString(nodeName);
		writer.writeString(host);
		writer.writeInt(port);
		writer.writeString(secretKey);
		writer.writeString(type.name());
	}
}
